package com.zhifa.hdtyg_cdgl.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.zhifa.hdtyg_cdgl.entity.SiteInformation;
import com.zhifa.hdtyg_cdgl.service.SiteInformationService;
import com.zhifa.hdtyg_cdgl.util.ComboboxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/siteInformation")
public class SiteInformationController {

    @Autowired
    private SiteInformationService siteInformationService;


    @RequestMapping("/findAllSiteInformation")
    public Map<String,Object> findAllSiteInformation(SiteInformation siteInformation, int page, int rows){
        PageInfo<SiteInformation> allSiteInformation = siteInformationService.findAllSiteInformation(siteInformation, page, rows);
        Map<String,Object>map=new HashMap<>();
        List<SiteInformation> list = allSiteInformation.getList();
        long total = allSiteInformation.getTotal();
        map.put("total",total);
        map.put("rows",list);
        return map;

    }
    @RequestMapping("/findName")
    public JSONArray findName(){

        List<String> name = siteInformationService.findName();
        JSONArray jsonArray = ComboboxUtil.changeListToJsonarray(name);

        return jsonArray;

    }
    @RequestMapping("/findPosition")
    public JSONArray findPosition(){
        List<String> position = siteInformationService.findPosition();
        JSONArray jsonArray=ComboboxUtil.changeListToJsonarray(position);


        return jsonArray;

    }
    @RequestMapping("/findPurpose")
    public JSONArray findPurpose(){
        List<String> purpose = siteInformationService.findPurpose();
        JSONArray jsonArray=ComboboxUtil.changeListToJsonarray(purpose);

        return jsonArray;
    }
    @RequestMapping("/findStatus")
    public JSONArray findStatus(){
        List<String> status = siteInformationService.findStatus();
        JSONArray jsonArray=ComboboxUtil.changeListToJsonarray(status);
        return jsonArray;
    }

    @RequestMapping("/addSiteInfo")
    public Map<String,Object> addSiteInfo(SiteInformation siteInformation, HttpSession httpSession){
        Map<String,Object> map=new HashMap<>();

        Integer adminId = (Integer) httpSession.getAttribute("user_id");

        siteInformation.setAdminId(adminId);
        siteInformation.setChangeDate(new Date());
        int info = siteInformationService.addSiteInfo(siteInformation);
        if (info>0){
            map.put("success",true);
        }else {
            map.put("success",false);
        }
        return map;
    }
    @RequestMapping("/modifySiteInfo")
    public Map<String,Object> modifySiteInfo(SiteInformation siteInformation, HttpSession httpSession){

        /**
         * 从session获取adminId
         */
        Integer adminId = (Integer) httpSession.getAttribute("user_id");

        siteInformation.setAdminId(adminId);
        siteInformation.setChangeDate(new Date());
        int info = siteInformationService.modifyInfo(siteInformation);
        Map<String,Object> map=new HashMap<>();
        if (info>0){
            map.put("success",true);
        }else {
            map.put("success",false);
        }
        return map;
    }



}
